<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/modules/user'

const router = useRouter()
const userStore = useUserStore()
const username = ref('')
const password = ref('')

const handleLogin = () => {
  if (userStore.login(username.value, password.value)) {
    // 根据用户角色跳转到不同页面
    if (userStore.userRole === 'admin') {
      router.push('/admin')
    } else {
      router.push('/visitor/map-browsing')
    }
  } else {
    ElMessage.error('用户名或密码错误')
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-left">

    </div>
    <div class="login-right">
      <div class="login-box">
        <h2>旅游道路设施管理系统</h2>
        <el-form>
          <el-form-item>
            <el-input
              v-model="username"
              placeholder="用户名"
              prefix-icon="User"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
        <!-- <div class="login-tips">
          <p>管理员账号：admin / 密码：123456</p>
          <p>访客账号：visitor / 密码：123456</p>
        </div> -->
      </div>
      <div class="login-footer">
        <div>山东交通学院公路事业发展中心</div>
        <div>2025 年</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  width: 100vw;
  height: 100%;
  display: flex;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.login-left {
  flex: 1;
  background: url('@/assets/login.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding-top: 5vh;
}

.login-right {
  width: 500px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 0 50px;
  background: #fff;
}

.login-title {
  font-size: 32px;
  color: white;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  text-align: center;
}

.login-box {
  padding: 40px 0;
  margin-top: 20vh;
}

.login-footer {
  color: #666;
  text-align: center;
  font-size: 14px;
  padding-bottom: 20px;
  line-height: 1.6;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

:deep(.el-input__wrapper) {
  background: #f5f7fa;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-button--primary) {
  background-color: #1890ff;
  height: 40px;
  font-size: 16px;
}

.login-tips {
  margin-top: 15px;
  color: #909399;
  font-size: 13px;
  text-align: center;
}

.login-tips p {
  margin: 5px 0;
}
</style>
